<template>
  <!--商品信息-->
  <div>
    <el-dialog
      :visible.sync="open"
      title="商品信息"
      width="800px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="100px"
        size="mini"
      >
      <el-form-item label="商品分类" prop="typeId">
          <el-select v-model="queryParams.typeId" placeholder="请选择商品分类" clearable size="small">
              <el-option
                v-for="item in productTypeList"
                :key="item.id"
                :label="item.typeName"
                :value="item.id"
              />
            </el-select>
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          clearable
          class="filter-item"
          style="width: 200px"
          placeholder="请输入商品名称"
        />
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          clearable
          class="filter-item"
          style="width: 200px"
          placeholder="请输入供应商名称"
        />
      </el-form-item>
      <el-form-item label="规格" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          clearable
          class="filter-item"
          style="width: 200px"
          placeholder="请输入规格"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilterProduct"
          size="mini"
          >查找</el-button
        ><el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
      </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
        :data="productList"
        element-loading-text="正在查询中。。。"
        border
        fit
        highlight-current-row
        ref="productListDetail"
        @selection-change="handleSelectionChange"
        @row-dblclick="dblclickSpecificationAdd"
        :load="loadData"
        lazy
        row-key="autoId"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column type="selection" width="50" align="center" fixed/>
        <el-table-column label="商品分类" align="center" prop="typeName" fixed/>
        <el-table-column label="商品编号" align="center" prop="productSn" min-width="120px"  fixed />
        <el-table-column label="商品名称" align="center" prop="productName" show-overflow-tooltip fixed/>
        <el-table-column label="规格" align="center" prop="itemName" min-width="130px" fixed/>
        <el-table-column label="是否包装类型" align="center" prop="isPack" min-width="130px" fixed :formatter="isPackFormat"/>
        <el-table-column label="计量单位" align="center" prop="measurement" min-width="120px" :formatter="measurementFormat"/>
        <el-table-column label="供应商名称" align="center" prop="supplierName" min-width="150px"/>
        <el-table-column label="品牌名称" align="center" prop="brandDesc" />
        <el-table-column label="参考单价" align="center" prop="referencePrice" v-if="productProps.priceShow"/>
        <el-table-column label="进货单价" align="center" prop="price" v-if="productProps.priceShow"/>
        <el-table-column label="零售价格" align="center" prop="marketPrice" v-if="productProps.marketPriceShow"/>
        <el-table-column label="当前库存数量" align="center" prop="storeCount" v-if="productProps.currentQtyShow" min-width="120px"/>
        <el-table-column label="保质期限" align="center" prop="shelfLife">
          <template slot-scope="scope">
            {{ scope.row.shelfLife }} {{ lifeTypeFormat(scope.row) }}
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getProductList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">取消</el-button>
        <el-button type="primary" @click="handleSpecificationAdd">确定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPrice } from "@/api/supplier/price";
import { listProductType } from "@/api/supplier/productType";
import { getProductId } from "@/api/supplier/productPack"
export default {
  name: "Product",
  props: {
    productProps: {
      open: false,
      productList: [],
      priceShow: false,
      marketPriceShow: false,
      currentQtyShow: false,
    },
    handleSelect: {
      type: Function
    },
    radiobox: true
  },
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        price: 0,
        marketPrice: 0,
        ids: [],
        orderByColumn: "productSort",
        isAsc: "asc"
      },
      open: this.productProps.open,
      // 总条数
      total: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的商品
      checkedProduct: [],
       // 计量单位字典
      measurementOptions: [],
      typeOptions:[],
      // 商品列表
      productList: [],
      // 商品分类表格数据
      productTypeList: [],
      // 保质期限年月日字典
      lifeTypeOptions: [],
    };
  },
  created() {
    
    this.getDicts("bus_life_type").then((response) => {
      this.lifeTypeOptions = response.data;
    });
    this.getDicts("bus_measurement").then(response => {
      this.measurementOptions = response.data;
    });
    listProductType().then(response => {
        this.productTypeList = response.rows;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.typeOptions = response.data;
    });
  },
  methods: {
     /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getProductList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询商品 */
    handleFilterProduct() {
      this.queryParams.pageNum = 1;
      this.getProductList();
    },
    loadData(tree, treeNode, resolve) {
      
      getProductId(tree.productId).then((response) => {
        setTimeout(() => {
          resolve(response.data)
        }, 1000)
      });
       
    },
    // 验货单状态字典翻译
    measurementFormat(row, column) {
      return this.selectDictLabel(this.measurementOptions, row.measurement);
    },
    isPackFormat(row,column){
      return this.selectDictLabel(this.typeOptions, row.isPack);
    },
    /** 查询商品列表 */
    getProductList() {

      this.loading = true;
     
      listPrice(this.queryParams).then((response) => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      if(!this.radiobox){
        this.checkedProduct = selection;
      }else{
        if(selection.length > 1){
          this.$refs.productListDetail.clearSelection()
          this.$refs.productListDetail.toggleRowSelection(selection.pop())
        }else{
          this.checkedProduct = selection;
        }  
      }
    },
    //在所有商品中选中对应商品进入订单列表的商品中
    handleSpecificationAdd() {
      this.checkedProduct.forEach((item) => {
        for (let i = 0; i < this.productList.length; i++) {
          if (this.productList[i].priceId === item.priceId) {

            this.productList.splice(i, 1);
            this.queryParams.ids.push(item.priceId);
            break;
          }
        }
      });
     
      this.handleSelect(this.checkedProduct)
      this.checkedProduct = [];

      this.open = false;
    },
    dblclickSpecificationAdd(row) {
      this.checkedProduct.push(row);
      this.handleSelect(this.checkedProduct);
      this.checkedProduct = [];

      this.open = false;
    },
    // 保质日期 年月日
    lifeTypeFormat(row, column) {
      return this.selectDictLabel(this.lifeTypeOptions, row.shelfLifeType);
    },
  },
};
</script>

<style>
</style>